﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace MVCGenerator
{
    public class CreateOrEditcshtml
    {
        private SpaceStackStringBuilder sb = new SpaceStackStringBuilder();

        public CreateOrEditcshtml(String NamespaceName, List<GridChoice> GridChoices, String PropertyClassName, bool UsingDropDownLists, EDMXParserV1.EntitySet SelectedEntitySet)
        {
            if (UsingDropDownLists)
            {
                sb.AppendLine("@model " + NamespaceName + ".Models." + PropertyClassName + "CreateOrEdit");
            }
            else
            {
                sb.AppendLine("@model " + NamespaceName + "." + PropertyClassName);
            }

            sb.AppendLine();
            sb.AppendLine("@* This partial view defines form fields that will appear when creating and editing entities *@");

            foreach (EDMXParserV1.Property Item in SelectedEntitySet.EntityTypeLink.Properties)
            {
                if (Item.IsDateTime())
                {
                    sb.AppendLine();
                    sb.AppendLine(@"<script type=""text/javascript"" language=""javascript"">");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine(@"$(document).ready(function () { ");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine();

                            sb.AppendLine(@"$('.date').datepicker({ ");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(@"dateFormat: ""mm/dd/yy"" ");
                                sb.RemoveSpace();
                            }

                            sb.AppendLine(@"});");

                        }
                        sb.RemoveSpace();
                        sb.AppendLine(@"});");
                    }
                    sb.RemoveSpace();
                    sb.AppendLine(@"</script>");
                    break;
                }
            }
            sb.AppendLine();
            foreach (EDMXParserV1.Property Item in SelectedEntitySet.EntityTypeLink.Properties)
            {

                if (Item.Name == SelectedEntitySet.EntityTypeLink.Keys[0])
                {
                    Debug.WriteLine(Item.Name);
                }
                else
                {

                    if (Item.IsInDetailView == "No")
                    {
                        // this column is not shown in the detail view.
                    }
                    else
                    {
                        sb.AppendLine(@"<div class=""editor-label"">");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("@Html.LabelFor(model => model." + Item.Name + ")");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("</div>");


                        sb.AppendLine(@"<div class=""editor-field"">");
                        if (true)
                        {
                            sb.AddSpace();

                            bool GridChoiceUsed = false;
                            foreach (GridChoice GridChoice in GridChoices)
                            {
                                if (GridChoice.ColumnName == Item.Name)
                                {
                                    sb.AppendStart();

                                    sb.Append(@"@Html.DropDownList( ""DDL" + Item.Name + @""", Model." + Item.Name + @"");

                                    if (Item.IsInDetailView == "View")
                                    {
                                        // disable the control
                                        sb.Append(@", new { disabled = ""disabled"", @readonly = ""readonly"" }");
                                    }

                                    sb.Append(")");
                                    sb.AppendLine();

                                    GridChoiceUsed = true;
                                    break;
                                }
                            }

                            if (GridChoiceUsed)
                            {

                            }
                            else
                            {
                                if (Item.IsInDetailView == "View")
                                {
                                    // just show the field
                                    sb.AppendLine("@Model." + Item.Name);
                                }
                                else
                                {
                                    if (Item.IsDateTime())
                                    {
                                        sb.AppendLine("@Html.TextBoxFor(model => model." + Item.Name + @", new { @class=""date"" })");
                                    }
                                    else
                                    {
                                        sb.AppendLine("@Html.TextBoxFor(model => model." + Item.Name + ")");
                                    }
                                }
                            }
                            sb.AppendLine("@Html.ValidationMessageFor(model => model." + Item.Name + ")");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("</div>");
                        sb.AppendLine("");
                    }

                }
            }
        }

        public override string ToString()
        {
            return sb.ToString();
        }
    }
}
